package com.example.service.impl;

import ch.qos.logback.classic.Logger;
import com.example.entity.supervision.CheckItem;
import com.example.mapp.CheckItemMapp;
import com.example.service.CheckItemService;
import com.example.util.ExcelUtil;
import com.example.util.ResultUtil;
import com.example.util.StringUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by simon on 2018/8/9.
 */
@Service
public class CheckItemServiceImpl implements CheckItemService {

    private final static Logger logger = (Logger) LoggerFactory.getLogger(CheckItemServiceImpl.class);

    @Autowired
    CheckItemMapp checkItemMapp;

    @Override
    public Map<String, Object> query(Map map) {
        try{
            Map map1 = new HashMap();
            List<CheckItem> list = checkItemMapp.query(map);
            map1.put("list",list);
            return ResultUtil.returnSuccess("成功", map1);
        }catch(Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError("失败", null);
        }
    }

    @Override
    public Map<String, Object> add(CheckItem map) {
        try{
            checkItemMapp.insert(map);
            return ResultUtil.returnSuccess("成功", null);
        }catch(Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError("失败", null);
        }
    }

    @Override
    public Map<String, Object> closeOrOpen(Map map) {
        try{
            checkItemMapp.closeOrOpen(map);
            return ResultUtil.returnSuccess("成功", null);
        }catch(Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError("失败", null);
        }
    }

    @Override
    public Map<String, Object> inExcel(MultipartFile file,String pDictId) {
        try {
            List<List<Object>> list = ExcelUtil.getListByExcel(file.getInputStream(), file.getOriginalFilename());
            //获取导入的文件
            if (list != null && list.size() > 0) {
                for (int i = 1; i < list.size(); i++) {
                    List listObject = list.get(i);
                    if (listObject != null && listObject.size() > 0 && StringUtil.isNotBlank(listObject.get(3)+"")) {
                        CheckItem checkItem = new CheckItem();
                        checkItem.setAdDictId(pDictId);
                        checkItem.setItemType("4");
                        checkItem.setItemName(listObject.get(1)+"");
                        checkItem.setItemNo(listObject.get(2)+"");
                        checkItem.setItemContent(listObject.get(3)+"");
                        checkItemMapp.insert(checkItem);
                    }
                }
            }
            return ResultUtil.returnSuccess("导入成功", null);
        }catch(Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError("导入失败", null);
        }
    }

    /**
     * 查询维保检查项
     * @param map
     * @return
     */
    @Override
    public Map<String, Object> querySpotCheckItem(Map map) {
        List<CheckItem> list = new ArrayList<>();
        try {
            list = checkItemMapp.querySpotCheckItem(map);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultUtil.returnError("失败", null);
        }
        return ResultUtil.returnSuccess("成功", list);
    }
}
